package com.woniuxy.empserver.controller;

import com.woniuxy.empserver.entity.TManager;
import com.woniuxy.empserver.servcie.ManagerService;
import com.woniuxy.empserver.util.JwtUtils;
import com.woniuxy.empserver.vo.AccountVo;
import com.woniuxy.empserver.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.management.relation.RelationSupport;

@RestController  // = @Controller +@ResponseBody
@RequestMapping("manager")
//@CrossOrigin //处理跨域
public class ManagerController {

    @Autowired
    JwtUtils jwtUtils;

    @Autowired
    ManagerService managerService ;

    @GetMapping("/login")
    public ResponseResult<TManager> login(String username ,String password){
        ResponseResult<TManager> result = new ResponseResult<>();
        try {
            TManager manager = managerService.login(username);
            if (manager == null) {
                result.setMsg("账户不存在!");
                result.setCode(201);
            }else {
                if (manager.getPassword().equals(password)) {
                    result.setMsg("登录成功!");
                    result.setCode(200);
                    result.setData(manager);
                    //生成 token  存储起来
                    AccountVo accountVo = new AccountVo();
                    accountVo.setUsername(username);
                    String token = jwtUtils.createToken(accountVo);
                    result.setToken(token);
                }else {
                    result.setMsg("密码错误!");
                    result.setCode(202);
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
            result.setMsg("系统错误，请联系管理员!");
            result.setCode(203);
        }
        return result ;
    }
}
